package com.yesep.learn.hik.utils;

import com.yesep.learn.hik.sdk.HCNetSDK;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: learn
 * @description: 转换海康时间
 * @author:
 * @create: 2019-09-10 10:03
 **/
public class DateUtil {

    public static HCNetSDK.NET_DVR_TIME convertTimeByDateTime(Date time) {
        Calendar calendar = new Calendar.Builder().build();
        calendar.setTime(time);

        HCNetSDK.NET_DVR_TIME dvrTime = new HCNetSDK.NET_DVR_TIME();
        dvrTime.dwYear = calendar.get(Calendar.YEAR);
        dvrTime.dwMonth = calendar.get(Calendar.MONTH) + 1;
        dvrTime.dwDay = calendar.get(Calendar.DATE);
        dvrTime.dwHour = calendar.get(Calendar.HOUR);
        dvrTime.dwMinute = calendar.get(Calendar.MINUTE);
        dvrTime.dwSecond = calendar.get(Calendar.SECOND);

        return dvrTime;
    }

    /**
     * 字符串转换成海康特有时间
     *
     * @param time
     * @return
     */
    public static HCNetSDK.NET_DVR_TIME convertTimeByString(String time) {
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return convertTimeByDateTime(parse);
    }


    public static HCNetSDK.NET_DVR_FILECOND convertDvrFilecondbyDateTime(Date startTime, Date endTime) {
        Calendar calendar = new Calendar.Builder().build();
        calendar.setTime(startTime);

        HCNetSDK.NET_DVR_FILECOND dvrFilecond = new HCNetSDK.NET_DVR_FILECOND();
        dvrFilecond.struStartTime = new HCNetSDK.NET_DVR_TIME();
        dvrFilecond.struStopTime = new HCNetSDK.NET_DVR_TIME();

        dvrFilecond.struStartTime.dwYear = calendar.get(Calendar.YEAR);
        dvrFilecond.struStartTime.dwMonth = calendar.get(Calendar.MONTH) + 1;
        dvrFilecond.struStartTime.dwDay = calendar.get(Calendar.DATE);
        dvrFilecond.struStartTime.dwHour = calendar.get(Calendar.HOUR);
        dvrFilecond.struStartTime.dwMinute = calendar.get(Calendar.MINUTE);
        dvrFilecond.struStartTime.dwSecond = calendar.get(Calendar.SECOND);

        Calendar calendar1 = new Calendar.Builder().build();
        calendar1.setTime(endTime);
        dvrFilecond.struStopTime.dwYear = calendar1.get(Calendar.YEAR);
        dvrFilecond.struStopTime.dwMonth = calendar1.get(Calendar.MONTH) + 1;
        dvrFilecond.struStopTime.dwDay = calendar1.get(Calendar.DATE);
        dvrFilecond.struStopTime.dwHour = calendar1.get(Calendar.HOUR);
        dvrFilecond.struStopTime.dwMinute = calendar1.get(Calendar.MINUTE);
        dvrFilecond.struStopTime.dwSecond = calendar1.get(Calendar.SECOND);

        return dvrFilecond;
    }

    public static HCNetSDK.NET_DVR_FILECOND convertDvrFilecondByString(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = sdf.parse(startTime);
            endDate = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return convertDvrFilecondbyDateTime(startDate, endDate);
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(convertTimeByString("2019-09-12 01:01:20"));

//        System.out.println(convertTimeByString("2019-9-11 14:25:30"));
//        System.out.println(convertTimeByDateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-9-11 14:25:30")));
    }
}
